package com.marvels.product.param.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.product.param.dto.McptPenaltyFeeDTO;
import com.marvels.product.param.service.PenaltyFeeService;

/**
 * 违约金页面
 * @author 宁华勇
 *
 */
@RestController
@RequestMapping("/product/param/penaltyfee/service")
public class PenaltyFeeServiceController {
	@Autowired
	private PenaltyFeeService penaltyFeeService;
	
	@RequestMapping("list")
	public MarvelsResultDTO list(@RequestBody Map<String,Object> param){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(param));
		
		PageInfo<McptPenaltyFeeDTO> pageInfo = null;
			
		try {
			pageInfo = penaltyFeeService.getPageInfoByParam(param);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageInfo);
	}
	
	
	@RequestMapping("getKey/{penaltyFeeId}")
	public MarvelsResultDTO edit(@PathVariable Long penaltyFeeId,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(penaltyFeeId));
		McptPenaltyFeeDTO entity = null;
		try {
			 entity = penaltyFeeService.getEntityByPrimaryKey(penaltyFeeId);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), entity);
	}
	
	
	@RequestMapping("save")
	public MarvelsResultDTO save(@RequestBody McptPenaltyFeeDTO dto,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setCreatedBy(userName);
			dto.setPenaltyFeeCode((MarvelsUtils.generateUUIDCode("YQ")));
			penaltyFeeService.insert(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dto);
	}
	
	@RequestMapping("update")
	public MarvelsResultDTO update(@RequestBody McptPenaltyFeeDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setUpdatedBy(userName);
			penaltyFeeService.updateByPrimaryKey(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	@RequestMapping("updateStatus")
	public MarvelsResultDTO updateStatus(@RequestBody McptPenaltyFeeDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			penaltyFeeService.updateStatus(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("违约金审核异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	@RequestMapping("delete")
	@ResponseBody
	public MarvelsResultDTO delete(@RequestBody McptPenaltyFeeDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			penaltyFeeService.delete(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("违约金删除异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
}
